package cn.bdqn.hospital.dao.impl;

import cn.bdqn.hospital.dao.JianChaDao;
import cn.bdqn.hospital.entity.JianCha;
import cn.bdqn.hospital.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 检查刀实现类
 */
public class JianChaDaoImpl implements JianChaDao {

    QueryRunner queryRunner = new QueryRunner();

    /**
     * 查询所有检查信息
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<JianCha> getAll() throws SQLException {
        List<JianCha> jcList = new ArrayList<JianCha>();
        try{
            String sql = "select * from JianCha;";
            jcList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<JianCha>(JianCha.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return jcList;
    }

    /**
     * 通过检查id获取信息
     *
     * @param jcId
     * @return
     * @throws SQLException
     */
    @Override
    public JianCha getJcByJcid(Integer jcId) throws SQLException {
        JianCha jc = null;
        try{
            String sql = "select * from JianCha where jcId = '?'";
            jc = (JianCha) queryRunner.query(sql,new BeanListHandler<JianCha>(JianCha.class),jcId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jc;
    }

    /**
     * 新增检查信息
     *
     * @param jc
     * @return
     * @throws SQLException
     */
    @Override
    public int add(JianCha jc) throws SQLException {
        String sql = "insert into JianCha(orderId,jcName,jcType,ddId,jcDate,jcResult,resultDate) values (?,?,?,?,?,?,?);";
        Object[] JianCha = new Object[]{jc.getOrderId(),jc.getJcName(),jc.getJcType(),jc.getDdId(),jc.getDdId(),jc.getJcResult(),jc.getResultDate()};
        return queryRunner.update(DruidUtils.getConnection(), sql, jc);
    }

    /**
     * 修改检查信息
     *
     * @param jc
     * @return
     * @throws SQLException
     */
    @Override
    public int update(JianCha jc) throws SQLException {
        int num = 0;
        String sql = "update JianCha set jcResult = ? where jcId = ?;";
        num = queryRunner.update(DruidUtils.getConnection(),sql,jc);
        return num;
    }

    /**
     * 删除检查信息
     *
     * @param jcId
     * @return
     * @throws SQLException
     */
    @Override
    public int delJcByJcid(Integer jcId) throws SQLException {
        int num = 0;
        try{
            String sql = "delete from JianCha where jcId='?';";
            num = queryRunner.update(DruidUtils.getConnection(),sql,jcId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 通过预约号查询检查信息
     *
     * @param orderId
     * @return
     * @throws SQLException
     */
    @Override
    public JianCha getJcByOrderid(Integer orderId) throws SQLException {
        JianCha jc = null;
        String sql = "select * from JianCha where orderId;";
        jc = (JianCha) queryRunner.query(sql,new BeanListHandler<JianCha>(JianCha.class),orderId);
        return jc;
    }

    /**
     * 通过缴费号查询检查信息
     *
     * @param ddId
     * @return
     * @throws SQLException
     */
    @Override
    public JianCha getJcByDdid(Integer ddId) throws SQLException {
        JianCha jc = null;
        String sql = "select * from JianCha where ddId;";
        jc = (JianCha) queryRunner.query(sql,new BeanListHandler<JianCha>(JianCha.class),ddId);
        return jc;
    }
}
